<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class FileVersion extends CMiniyunModel
{

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return Yii::app()->params['tablePrefix'].'file_versions';
    }
    
    public function deleteFileVersion($ids){
        if($ids!='' && strlen($ids)>0){
            $idsArray = explode(",",$ids);
            foreach($idsArray as $index=>$versionId){
                                $this->updateAll(array("ref_count"=>"ref_count-1"), "id=".$versionId);
            }
        }
    }
    
    public function fileSum(){
        $dbCommand = Yii::app()->db->createCommand("SELECT sum(file_size) as usedSpace  FROM `".Yii::app()->params['tablePrefix']."file_versions`");
        $data =  $dbCommand->queryAll();
        foreach($data as $index=>$item){
            return $item["usedSpace"];
        }
    }

    
    public function createFileVersion($signature, $size, $mime_type){
        $fileVersion = new FileVersion();
        $fileVersion->file_signature = $signature;
        $fileVersion->file_size = $size;
        $fileVersion->block_ids = 0;
        $fileVersion->ref_count = 0;
        $fileVersion->mime_type = $mime_type;
        $fileVersion->save();
        return $fileVersion;
    }
    
    
    
    
    public function queryFileVersionBySignatrue($signatrue) {
        $fileVersion = $this->find(array('condition'=>'file_signature=:file_signature','params'=>array(':file_signature'=>$signatrue)));
        return $fileVersion;
    }

    public function officeIds()
    {
                                $condition='';
        $params=array();
        foreach (Yii::app()->params["officeType"] as $key=>$item)
        {
            $condition.='mime_type=:'.$key.' or ';
            $params[$key] = $item;
        }
                                $condition = substr($condition, 0, count($condition) - 4);

        $model = $this->findAll($condition, $params);

        return $model;
    }
    
    public function imageIds()
    {
        return $this->findAll('mime_type like :type', array('type'=>'image%'));
    }
    
    public function videoIds()
    {
        return $this->findAll('mime_type like :type', array('type'=>'video%'));
    }
    
    public function audioIds()
    {
        return $this->findAll('mime_type like :type', array('type'=>'audio%'));
    }
}